package com.kcyu.sfherp.service.impl;

import com.kcyu.sfherp.entity.CkSale;
import com.kcyu.sfherp.entity.CkSaleLine;
import com.kcyu.sfherp.mapper.ck.CkSaleLineMapper;
import com.kcyu.sfherp.mapper.ck.CkSaleMapper;
import com.kcyu.sfherp.service.CkService;
import com.kcyu.sfherp.utils.RedisCache;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class CkServiceImpl implements CkService {

    @Autowired
    private CkSaleMapper ckSaleMapper;

    @Autowired
    private CkSaleLineMapper ckSaleLineMapper;

    @Autowired
    private RedisCache redisCache;

    @Override
    public List<CkSale> queryTodayCkSale() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String today = format.format(date);
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, 1);
        String tomorrow = format.format(calendar.getTime());
        List<CkSale> ckSales = ckSaleMapper.queryByTime(today, tomorrow);
        return ckSales;
    }

    @Override
    public List<CkSale> queryNoneNoticeSales() {
        List<CkSale> noneNoticeSales = new ArrayList<>();
        Set<String> sentSales = querySentSales();
        List<CkSale> ckSales = queryTodayCkSale();
        for (CkSale ckSale: ckSales) {
            String billID = ckSale.getBillID();
            if(sentSales.contains(billID)) {
                continue;
            } else {
                noneNoticeSales.add(ckSale);
                addSentSales(ckSale);
            }
        }
        return noneNoticeSales;
    }

    @Override
    public List<CkSaleLine> querySingleSale(String billId) {
        return ckSaleLineMapper.queryByBillID(billId);
    }

    @Override
    public Set<String> querySentSales() {
        Set<String> noticedSales = redisCache.getCacheSet("noticedSales");
        return noticedSales;
    }

    public void addSentSales(CkSale cksale) {
        Set<String> noticedSales = redisCache.getCacheSet("noticedSales");
        noticedSales.add(cksale.getBillID());
        redisCache.setCacheSet("noticedSales", noticedSales);
    }


}
